//
//  mimaViewController.m
//  shuilijuOASystem
//
//  Created by michael on 17/1/5.
//  Copyright © 2017年 czd. All rights reserved.
//
#import "Toast+UIView.h"
#import "UIViewExt.h"
#import "mimaViewController.h"
#import <CommonCrypto/CommonDigest.h>
#import "Masonry.h"
static int countDownTime = 180;

@interface mimaViewController ()
{
    UIImageView *View;
    UIView *bgView;
   

}
@end

@implementation mimaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:

[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0a62ea"];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    self.title = NSLocalizedString(@"修改密码", nil);
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
      self.navigationController.navigationBar.hidden = NO;

    //
    [self layoutRootViewControllerSubviews];
       //点击手势
 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keybordDown)];
    [self.view addGestureRecognizer:tap];

//        self.navigzationItem.rightBarButtonItem = rightIem;
//    [self createTextFields];

    // Do any additional setup after loading the view.
     UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"]style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
 self.navigationItem.leftBarButtonItem = item1;
}
- (void)buttonClick{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{

//self.navigationController.navigationBarHidden = NO;
}
- (void)changeTFUserInteractionEnabled:(BOOL)boolFlag{
    self.resetPassword.userInteractionEnabled = boolFlag;
    self.repetNewPassword.userInteractionEnabled = boolFlag;
}
#pragma mark - 基本设置
//- (void)config{
//    self.navigationItem.title = @"重置密码";
//    if (!kIsValidNSString(self.originPassword.text) || !self.originPassword.text) {
//        [self changeTFUserInteractionEnabled:NO];
//    }else{
//        [self changeTFUserInteractionEnabled:YES];
//    }
//    //
//    [self.originPassword addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//}
#pragma mark - 布局
- (void)layoutRootViewControllerSubviews{
  
              _originPassword.keyboardType = UIKeyboardTypeASCIICapable;
              _resetPassword.keyboardType = UIKeyboardTypeASCIICapable;
              _repetNewPassword.keyboardType = UIKeyboardTypeASCIICapable;
[_resetPassword setSecureTextEntry:YES];
[_repetNewPassword setSecureTextEntry:YES];
  UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 40)];
  label.text = NSLocalizedString(@"当前密码:", nil);
  label.backgroundColor = [UIColor whiteColor];
  label.textColor = [UIColor colorWithHexString:@"484848"];
  label.font = [UIFont systemFontOfSize:14];
  [self.view addSubview:label];

  UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, label.bottom +20, 80, 40)];
    label2.backgroundColor = [UIColor whiteColor];
  label2.text = NSLocalizedString(@"新密码:", nil);
  label2.textColor = [UIColor colorWithHexString:@"484848"];
  label2.font = [UIFont systemFontOfSize:14];
  [self.view addSubview:label2];

   UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, label2.bottom +1, 80, 40)];
     label3.backgroundColor = [UIColor whiteColor];
  label3.text = NSLocalizedString(@"确认密码:", nil);
  label3.textColor = [UIColor colorWithHexString:@"484848"];
  label3.font = [UIFont systemFontOfSize:14];
  [self.view addSubview:label3];
  
   UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(0, label3.bottom +20, 80, 40)];
   label4.backgroundColor = [UIColor whiteColor];

  label4.text = NSLocalizedString(@"验证码:", nil);
  label4.textColor = [UIColor colorWithHexString:@"484848"];
  label4.font = [UIFont systemFontOfSize:14];
//  [self.view addSubview:label4];


   _originPassword = [[UITextField alloc]init];
          _originPassword.keyboardType = UIKeyboardTypeNumberPad;

  _originPassword.frame = CGRectMake(label.right, label.top, KScreenWidth-80, 40);
   UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _originPassword.placeholder = NSLocalizedString(@"请输入旧密码", nil);
    [_originPassword setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
[_originPassword setValue:[UIColor colorWithHexString:@"a1a1a1"] forKeyPath:@"_placeholderLabel.textColor"];
  _originPassword.backgroundColor = [UIColor whiteColor];

_originPassword.secureTextEntry = YES;
  [_originPassword setFont:[UIFont systemFontOfSize:13]];
  _originPassword.textAlignment = NSTextAlignmentLeft;
  _originPassword.delegate = self;
   [self.view addSubview:_originPassword];

_resetPassword = [[UITextField alloc]init];
          _resetPassword.keyboardType = UIKeyboardTypeNumberPad;

  _resetPassword.frame = CGRectMake(label2.right, label2.top, KScreenWidth-80, 40);
  _resetPassword.placeholder =NSLocalizedString(@"密码是6-20位的数字,字母和符号组成", nil);
     [_resetPassword setValue:[UIFont boldSystemFontOfSize:11] forKeyPath:@"_placeholderLabel.font"];
_resetPassword.backgroundColor = [UIColor whiteColor];

[_resetPassword setValue:[UIColor colorWithHexString:@"a1a1a1"] forKeyPath:@"_placeholderLabel.textColor"];
   
  [_resetPassword setFont:[UIFont systemFontOfSize:13]];
  _resetPassword.textAlignment = NSTextAlignmentLeft;
  _resetPassword.delegate = self;
   [self.view addSubview:_resetPassword];

_repetNewPassword = [[UITextField alloc]init];
          _repetNewPassword.keyboardType = UIKeyboardTypeNumberPad;

  _repetNewPassword.frame = CGRectMake(label.right, label3.top+1, KScreenWidth-80, 40);
  _repetNewPassword.placeholder = NSLocalizedString(@"请再次输入密码", nil);
                  [_repetNewPassword setValue:[UIFont boldSystemFontOfSize:11] forKeyPath:@"_placeholderLabel.font"];

[_repetNewPassword setValue:[UIColor colorWithHexString:@"a1a1a1"] forKeyPath:@"_placeholderLabel.textColor"];
_repetNewPassword.backgroundColor = [UIColor whiteColor];
   _repetNewPassword.secureTextEntry = YES;
  [_repetNewPassword setFont:[UIFont systemFontOfSize:13]];
  _repetNewPassword.textAlignment = NSTextAlignmentLeft;
  _repetNewPassword.delegate = self;
   [self.view addSubview:_repetNewPassword];

 _yanzhengPassword = [[UITextField alloc]init];
          _yanzhengPassword.keyboardType = UIKeyboardTypeNumberPad;

  _yanzhengPassword.frame = CGRectMake(label4.right, label4.top, KScreenWidth-80, 40);
      _yanzhengPassword.placeholder = NSLocalizedString(@"请输入旧密码", nil);
    [_yanzhengPassword setValue:[UIFont boldSystemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
[_yanzhengPassword setValue:[UIColor colorWithHexString:@"a1a1a1"] forKeyPath:@"_placeholderLabel.textColor"];
  _yanzhengPassword.backgroundColor = [UIColor whiteColor];

//_yanzhengPassword.secureTextEntry = YES;
  [_yanzhengPassword setFont:[UIFont systemFontOfSize:13]];
  _yanzhengPassword.textAlignment = NSTextAlignmentLeft;
  _yanzhengPassword.delegate = self;
//   [self.view addSubview:_yanzhengPassword];

_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setTitle:NSLocalizedString(@"确认修改", nil) forState:UIControlStateNormal];
[_button addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
_button.frame = CGRectMake(20, label4.bottom+20, KScreenWidth-40, 40);
_button.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];
  [self.view addSubview:_button];
         _yanzbutton = [myView creatButtonWithFrame:CGRectMake(_yanzhengPassword.width-100, 5,100,30 ) title:@"获取验证码" tag:1000 tintColor:[UIColor whiteColor] backgroundColor:[UIColor colorWithHexString:@"0aa0f6"]];
        _yanzbutton.layer.cornerRadius = 10;
        [_yanzbutton addTarget:self action:@selector(getVerificationCode) forControlEvents:UIControlEventTouchUpInside];
//        [_yanzhengPassword addSubview:_yanzbutton];
}

- (void)getVerificationCode
{
 if ([_originPassword.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"]]) {

  if ([_resetPassword.text isEqualToString:_repetNewPassword.text]) {
}else{



    _label = [[UILabel alloc]initWithFrame:self.yanzbutton.bounds];
    
    _timer = [[NSTimer alloc]init];
    _timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantPast]];
    //请求服务器发送短信验证码
    //......
  
}
}

}
- (void)countDown
{
    countDownTime--;
    if (countDownTime<=0||countDownTime ==180) {
        _label.hidden = YES;
        _yanzbutton.hidden = NO;
        _yanzbutton.userInteractionEnabled = YES;
        [_timer setFireDate:[NSDate distantFuture]];
        countDownTime =180;
    }else{

        _label.hidden = NO;
        _label.text = [NSString stringWithFormat:@"(%ds)%@",countDownTime,NSLocalizedString(@"确认修改", nil)];
        _label.font = [UIFont systemFontOfSize:12];
        _label.backgroundColor = [UIColor colorWithRed:18/255.0  green:129/255.0  blue:201/255.0 alpha:1];
        _label.layer.cornerRadius = 8;
        _label.clipsToBounds = YES;
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor= [UIColor whiteColor];
        [_yanzbutton addSubview:_label];
        _yanzbutton.userInteractionEnabled = NO;
    }
}
- (void)keybordDown {
      [self.view endEditing:YES];
}
- (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
- (void)loginAction {
NSString *oldstring=[_originPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *nowstring=[_resetPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *nowstring2=[_repetNewPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *yanzhengstring=[_yanzhengPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([oldstring isEqualToString:@""]|| oldstring.length == 0) {
        [self.view makeToast:NSLocalizedString(@"请输入旧密码", nil)];
}else if ([nowstring isEqualToString:@""]|| nowstring.length == 0) {
        [self.view makeToast:NSLocalizedString(@"请输新密码", nil)];
}else if ([nowstring2 isEqualToString:@""]|| nowstring2.length == 0) {
        [self.view makeToast:NSLocalizedString(@"请重新输入密码", nil)];
}
//else if ([yanzhengstring isEqualToString:@""]|| yanzhengstring.length == 0) {
//        [self.view makeToast:NSLocalizedString(@"请输入验证码", nil)];
//}
else{

 if ([_originPassword.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"pwdId"]]) {

  if ([_resetPassword.text isEqualToString:_repetNewPassword.text]) {

  [self loaddata];


  }else{

          [self.view makeToast:NSLocalizedString(@"请确保两次输入的新密码一致", nil)];


  }



}else{


        [self.view makeToast:NSLocalizedString(@"旧密码输入错误!", nil)];


}



}

}

-(void)loaddata{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
     NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
     NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
  NSString *newpassword =  [ self md5:_resetPassword.text];

NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];

NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>%@</userName><password>%@</password><website>%@</website><language>%@</language><type>%@</type></header><body><username>%@</username><oldpassword>%@</oldpassword><newpassword>%@</newpassword><pscode>%@</pscode></body></message>",name,password,website,language,BAtype,name,password,newpassword,_resetPassword.text];
 NSArray * array = @[@{@"arg0":bodyStr}];
    NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bEditPassword];
     [HttpTools requestWithURL:path arrays:array method:@"EditPassword" success:^(id JSON) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    NSLog(@"%@",JSON);
         [self.view makeToast:JSON[@"msg"]];
         if ([[NSString stringWithFormat:@"%@",JSON[@"code"]]isEqualToString:@"200"]) {
    //成功
}

      
    } failure:^(NSError *error) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
